import time

import win32api
import win32gui
import win32con


class Upload_window:

    @staticmethod
    def send_enter_key(hwnd):
        # 发送回车键按下消息
        win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        # 发送回车键松开消息
        win32gui.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)

    # 只读变更可编辑
    @staticmethod
    def make_editbox_editable(hwnd):
        print("句柄调整可编辑样式：", hwnd)
        # 获取当前样式
        style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)
        # 清除只读样式
        if style & win32con.ES_READONLY:
                print("清除只读样式")
                style = style & (~win32con.ES_READONLY)
                # 设置新的样式
                win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE, style)

    # 模拟鼠标点击
    @staticmethod
    def  mouse_left_click():
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        time.sleep(0.1)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

    # 获取句柄坐标
    @staticmethod
    def rect_x_y(dialog):
        rect = win32gui.GetWindowRect(dialog)
        # 转化 x y 坐标
        x = (rect[0] + rect[2]) // 2
        y = (rect[1] + rect[3]) // 2
        return x, y

    @staticmethod
    def upload_file(file_path, file_name):
        print("============================开始上传============================")
        print("获取window弹出窗口")
        dialog = win32gui.FindWindow(None, "打开")
        # 页面提权
        print("window窗口提权")
        win32gui.SetForegroundWindow(dialog)
        # 获取目录句柄
        print("获取目录句柄")
        worker_w = win32gui.FindWindowEx(dialog, 0, 'WorkerW', None)
        re_bar_window32 = win32gui.FindWindowEx(worker_w, 0, 'ReBarWindow32', None)
        address_band_root = win32gui.FindWindowEx(re_bar_window32, 0, 'Address Band Root', None)
        catalog_msctls_progress32 = win32gui.FindWindowEx(address_band_root, 0, 'msctls_progress32', None)
        catalog_combo_box_ex32 = win32gui.FindWindowEx(catalog_msctls_progress32, 0, 'ComboBoxEx32', None)
        catalog_comb_box = win32gui.FindWindowEx(catalog_combo_box_ex32, 0, 'ComboBox', None)
        catalog_edit = win32gui.FindWindowEx(catalog_comb_box, 0, 'Edit', None)
        time.sleep(1)
        # 鼠标指到对应位置
        print("鼠标指到对应位置")
        win32api.SetCursorPos((Upload_window.rect_x_y(address_band_root)))
        time.sleep(1)
        # 鼠标点击
        print("鼠标点击目录输入框")
        Upload_window.mouse_left_click()
        time.sleep(1)
        # 获取焦点后给定目标路径
        print("获取焦点后给定目标路径")
        win32gui.SendMessage(catalog_edit, win32con.WM_SETTEXT, None, file_path)
        time.sleep(1)
        # 回车
        print("回车")
        Upload_window.send_enter_key(catalog_edit)
        time.sleep(1)
        # 文件查找框
        print("获取文件查找框句柄")
        # button = win32gui.FindWindowEx(dialog, 0, 'Button', '取消')
        combo_box_ex_32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
        combo_box = win32gui.FindWindowEx(combo_box_ex_32, 0, 'ComboBox', None)
        edit = win32gui.FindWindowEx(combo_box, 0, 'Edit', None)
        print("输入文件名")
        win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_name)
        # 打开(&o) 获取定位打开按钮
        print("获取打开操作按钮句柄")
        button = win32gui.FindWindowEx(dialog, 0, 'Button', '打开(&o)')
        win32api.SetCursorPos((Upload_window.rect_x_y(button)))
        time.sleep(1)
        # 模拟点击
        print("点击打开按钮")
        Upload_window.mouse_left_click()
        print("============================上传完成============================")
